uniform vec2 chunk_pos;
uniform float global_illum;
uniform float wind;

vec3 vVertex;
vec3 eyeVec;
vec3 N;

vec4 calculateLight(ge_struct_Light light, float spec){
	vec4 final_color = light.ambient;
	if(light.attenuation == -1.0){
		return vec4(0.0);
	}else{
		vec3 L = normalize(light.vector - vVertex);
		float lambertTerm = dot(N, L);
		if(lambertTerm > 0.0){
			final_color += light.diffuse * lambertTerm;
			if(spec > 0.0){
				vec3 E = normalize(eyeVec);
				vec3 R = reflect(-L, N);
				float specular = pow(max(dot(R, E), 0.0), 2.0);
				final_color += spec * vec4(specular);
			}
		}
	}

	final_color.a = 1.0;
	return final_color;
}

void main(){
	vec4 pos = ge_VertexPosition;
	pos.x = mod(ge_VertexPosition.x, 100.0);
	pos.xy += chunk_pos;
	ge_Color = ge_VertexColor;

	N = ge_MatrixNormal * normalize(ge_VertexNormal);
	vVertex = vec3(ge_MatrixModelView * pos);
	eyeVec = -vVertex;

	float local_light = floor(ge_VertexPosition.x / 1000.0);
	ge_Color.rgb *= clamp(calculateLight(ge_StaticLights[0], 0.0).rgb * global_illum + local_light, vec3(0.0), vec3(1.0));

	ge_Position = ge_MatrixProjection * ge_MatrixModelView * pos;
}
